package com.sfx.concurrent.demo.concurrent.AtomicTest.Account;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-19
 * Time: 19:09
 */
public interface Account {

    //获取金额
    Integer getBalance();

    //取款
    void withDraw(int account);

    /**
     * 假设有10000块钱,有1000个线程执行取款操作,每一个线程取款10元
     */
    static void demo(Account account) {
        List<Thread> list = new ArrayList<>();
        long start = System.nanoTime();

        for(int i =0;i<1000;++i) {
            list.add(new Thread(()->{
                account.withDraw(10);
            }));
        }
        list.forEach(Thread::start);

        list.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.nanoTime();

        System.out.println("剩余钱 : " + account.getBalance()
        + "执行时间 : " + (end-start)/1000_000);
    }
}
